package controller.subsystemcom;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import controller.acquaintance.I_WorkCellController_PackML;
import controller.acquaintance.I_MesLogs;
import controller.acquaintance.I_WorkCellController_PackmlHandler;
import controller.model.WorkCellControllerServer;

public class PackML implements I_MesLogs, I_WorkCellController_PackmlHandler
{
	private static PackML instance;
	private I_WorkCellController_PackML workCellController;
	private I_WorkCellController_PackmlHandler packmlHandlerworkCellController;
	
	private PackML()
	{
		try
		{
			workCellController = (I_WorkCellController_PackML) Naming.lookup(WorkCellControllerServer.rmiPackML);
			packmlHandlerworkCellController = (I_WorkCellController_PackmlHandler) Naming.lookup(WorkCellControllerServer.rmiPackMLHandler);
		}
		catch (MalformedURLException e)
		{ e.printStackTrace(); }
		catch (RemoteException e)
		{ e.printStackTrace(); }
		catch (NotBoundException e)
		{ e.printStackTrace(); }
	}
	
	public static PackML getInstance()
	{
		if (instance == null)
			instance = new PackML();
		return instance;
	}
	
	public void sendLogPML_Aborted(long time, String comment)
	{
		try
		{
			workCellController.sendLogPML_Aborted(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }
	}
	
	public void sendLogPML_Stopped(long time, String comment)
	{
		try
		{
			workCellController.sendLogPML_Stopped(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }			
	}

	public void sendLogPML_Idle(long time, String comment)
	{
		try
		{
			workCellController.sendLogPML_Idle(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }		
	}

	public void sendLogPML_Execute(long time, String comment)
	{
		try
		{
			workCellController.sendLogPML_Execute(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }
	}

	public void sendLogPML_Complete(long time, String comment)
	{
		try
		{
			workCellController.orderCompleted(time);
			workCellController.sendLogPML_Complete(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }				
	}

	public void sendLogPML_Held(long time, String comment)
	{
		try
		{
			workCellController.sendLogPML_Held(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }		
	}

	public void sendLogPML_Suspended(long time, String comment)
	{
		try
		{
			workCellController.sendLogPML_Suspended(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }		
	}

	public void sendLogOrder_Start(long time, String comment)
	{
//		try
//		{
//			workCellController.sendLogOrder_Start(time, "");
//		}
//		catch (RemoteException e)
//		{ e.printStackTrace(); }		 
	}

	public void PML_state_Stopping(long time, String comment)
	{
		try
		{
			packmlHandlerworkCellController.PML_state_Stopping(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }				
	}

	public void PML_state_Resetting(long time, String comment)
	{
		try
		{
			packmlHandlerworkCellController.PML_state_Resetting(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }				
	}

	public void PML_state_Starting(long time, String comment)
	{
		// Nothing happens. Start button on PackML interface is pressed once.
		// When pressed the second time sendLogPML_Execute is called.			
	}

	public void PML_state_Holding(long time, String comment)
	{
		try
		{
			packmlHandlerworkCellController.PML_state_Holding(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }					
	}

	public void PML_state_UnHolding(long time, String comment)
	{
		try
		{
			packmlHandlerworkCellController.PML_state_UnHolding(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }				
	}

	public void PML_state_Completing(long time, String comment)
	{
		try
		{
			packmlHandlerworkCellController.PML_state_Completing(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }				
	}

	public void PML_state_Suspending(long time, String comment)
	{
		try
		{
			packmlHandlerworkCellController.PML_state_Suspending(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }				
	}

	public void PML_state_UnSuspending(long time, String comment)
	{
		try
		{
			packmlHandlerworkCellController.PML_state_UnSuspending(time, "");
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }				
	}

	public void fetchNewOrder()
	{
		try 
		{
			workCellController.fetchNewOrder();
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }	
	}

	public long getTimeSinceLastBrick()
	{
		long toReturn = 0;
		try
		{
			toReturn = workCellController.getTimeSinceLastBrick();
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }
		
		return toReturn;
	}

	public boolean isCurrentOrderCompleted()
	{
		boolean toReturn = false;
		try
		{
			toReturn = workCellController.isCurrentOrderCompleted();
		}
		catch (RemoteException e)
		{ e.printStackTrace(); }
		
		return toReturn;
	}
}